package com.miaoxie.forum.module.im.server.service.impl;

import com.miaoxie.forum.module.im.server.dao.IIMUserDao;
import com.miaoxie.forum.module.im.server.service.IIMService;
import com.miaoxie.forum.module.im.shared.entity.IMInitEntity;
import com.miaoxie.forum.module.im.shared.entity.IMInitUsers;
import com.miaoxie.forum.module.im.shared.entity.IMInitfrend;
import com.miaoxie.forum.module.im.shared.entity.IMUsersEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by miaoxie on 2017/8/19.
 */
@Service
public class IMService implements IIMService {
    @Autowired
    IIMUserDao userDao;
    @Override
    public IMInitEntity initIm(String userid) {
        IMInitEntity imInitEntity = new IMInitEntity();
        IMUsersEntity usersEntity;
        try {
            usersEntity = userDao.getUserByNo(userid);
            imInitEntity.setCode(0);
            imInitEntity = getinitEntity(usersEntity,imInitEntity);
        }catch (Exception ex){
            imInitEntity.setCode(1);
            imInitEntity.setMsg("数据查询失败");
            ex.printStackTrace();
        }

        return imInitEntity;
    }

    private IMInitEntity getinitEntity(IMUsersEntity entity,IMInitEntity imInitEntity){
        IMInitUsers inituser = new IMInitUsers(entity);
        inituser.setStatus("online");
        imInitEntity.dataMapPut("mine",inituser);
        List<IMInitfrend> friend = new ArrayList<>();
        for (int i=0;i<entity.getGroupEntities().size();i++){
            IMInitfrend initfrend = new IMInitfrend(entity.getGroupEntities().get(i),i+1);

            friend.add(initfrend);
        }
        imInitEntity.dataMapPut("friend",friend);
        return imInitEntity;
    }
}
